package com.itzhigui.controller.admin;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.itzhigui.entity.User;
import com.itzhigui.service.UserService;
import com.itzhigui.utils.JWTUtil;
import com.itzhigui.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台调用的接口
 */
@RestController
@Slf4j
//@CrossOrigin
@RequestMapping(value = "/admin/user")
@Api(value = "后台用户接口" ,tags = "AdminUserController") // 对控制器的描述
public class AdminUserController {
    @Resource
    private UserService userService;

    //在swagger2中对接口的描述，value属性是简单的描述，notes:详细描述
    @ApiOperation(value = "获取所有用户" ,notes = "后台获取所有的用户详细信息")
    @GetMapping(value = "/find/{page}/{pageSize}")
    public ResponseUtil find(
            @PathVariable(value = "page") Integer page,
            @PathVariable(value = "pageSize") Integer pageSize
    ){
        return userService.find(page,pageSize);
    }
    //在swagger2中对接口的描述，value属性是简单的描述，notes:详细描述
    @ApiOperation(value = "添加用户" ,notes = "后台添加用户信息")
    @ApiImplicitParam(value = "用户对象数据" ,name = "user" ,required = true,dataType = "User")
    @PostMapping(value = "/add")
    public ResponseUtil add(@RequestBody User user){
        return userService.add(user);
    }

    //编写后台的登录接口
    @ApiOperation(value = "用户登录" ,notes = "后台登录接口")
    @ApiImplicitParam(value = "用户对象数据" ,name = "user" ,required = true,dataType = "User")
    @PostMapping(value = "/login")
    public  Map<String, Object> login(
            @RequestBody User user
    ){
        ResponseUtil r = userService.fondAdminByName(user);
         Map<String,Object> map = new HashMap<>();
        if (r.getCode().intValue() == 1012) {
            //登录成功
            User loginUser = (User) r.getData();
            //生成jwt令牌
             Map<String,String> payload = new HashMap<>();
             payload.put("userId", loginUser.getId().toString());
             payload.put("username", loginUser.getUsername());
            String token = JWTUtil.getToken(payload);
            map.put("stats", true);
            map.put("msg", "认证成功");
            map.put("token", token);
        } else {
            map.put("stats", false);
            map.put("msg", r.getMsg());
        }
        return map;
    }

    //测试令牌的接口
//    @GetMapping("/test")
//    public Map<String,Object> test(String token){
//         Map<String,Object> map = new HashMap<>();
//          try {
//              DecodedJWT decodedJWT = JWTUtil.verify(token);
//              //没有异常，验签是通过的
//              map.put("state", true);
//              map.put("msg", "请求成功");
//              return map;
//          } catch (SignatureVerificationException e) {
//              e.printStackTrace();
//              map.put("msg", "无效签名!");
//          } catch (TokenExpiredException e) {
//              e.printStackTrace();
//              map.put("msg", "token过期!");
//          } catch (AlgorithmMismatchException e) {
//              e.printStackTrace();
//              map.put("msg", "token算法不一致!");
//          } catch (Exception e) {
//              e.printStackTrace();
//              map.put("msg", "token无效");
//          }
//        map.put("stats", false);
//        return map;
//    }

    @GetMapping("/test")
    public Map<String, Object> test(String token) {
        Map<String, Object> map = new HashMap<>();
        map.put("state", true);
        map.put("msg", "请求成功");
        return map;
    }
}
